package com.ctbu.yyx.cameracontrol.webapi;

import com.ctbu.yyx.cameracontrol.core.PageUtils;
import com.ctbu.yyx.cameracontrol.domain.Camera;
import com.ctbu.yyx.cameracontrol.domain.User;
import com.ctbu.yyx.cameracontrol.service.CameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.DoubleToIntFunction;

@RestController
@RequestMapping("/webapi")
public class CameraRestController {
    @Autowired
    private CameraService cameraService;

    @PostMapping("/caneraxinxi")
    public int caneraxinxi (Camera camera){

        List<Camera> cameras = cameraService.findByIp(camera.getIp());
        if(cameras.size()>0){
            return -1;
        }else{
            Camera camera1 = cameraService.insert(camera);
            return 1;
        }


    }
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size){

        Sort sort=Sort.by(Sort.Direction.ASC,"id");
        Page<Camera> cameraPage;
        Pageable pageable = PageRequest.of(page,size,sort);
        cameraPage = cameraService.findAll(pageable);
        System.out.println(cameraPage.getSize());
        PageUtils pageUtils = new PageUtils(cameraPage.getContent(),cameraPage.getTotalElements());
        return pageUtils;

    }
}
